home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / MAP.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  2KB  |  70 lines

  1.  #include <string>
  2.  #include <map>
  3.  
  4.  using namespace std;
  5.  
  6.  typedef map<string, int, less<string> > months_type;
  7.  
  8.  //
  9.  // Print out a pair.
  10.  //
  11.  template <class First, class Second>
  12.  ostream& operator<< (ostream& out, const pair<First,Second> & p)
  13.  {
  14.    cout << p.first << " has " << p.second << " days";
  15.    return out;
  16.  }
  17.  
  18.  //
  19.  // Print out a map.
  20.  //
  21.  ostream& operator<< (ostream& out, const months_type & l)
  22.  {
  23.    copy(l.begin(),l.end(), ostream_iterator
  24.                 <months_type::value_type>(cout,"\n"));
  25.    return out;
  26.  }
  27.  
  28.  int main ()
  29.  {
  30.    //
  31.    // Create a map of months and the number of days in the month.
  32.    //
  33.    months_type months;
  34.  
  35.    typedef months_type::value_type value_type;
  36.    //
  37.    // Put the months in the multimap.
  38.    //
  39.    months.insert(value_type(string("January"),   31));
  40.    months.insert(value_type(string("Febuary"),   28));
  41.    months.insert(value_type(string("Febuary"),   29));
  42.    months.insert(value_type(string("March"),     31));
  43.    months.insert(value_type(string("April"),     30));
  44.    months.insert(value_type(string("May"),       31));
  45.    months.insert(value_type(string("June"),      30));
  46.    months.insert(value_type(string("July"),      31));
  47.    months.insert(value_type(string("August"),    31));
  48.    months.insert(value_type(string("September"), 30));
  49.    months.insert(value_type(string("October"),   31));
  50.    months.insert(value_type(string("November"),  30));
  51.    months.insert(value_type(string("December"),  31));
  52.    //
  53.    // Print out the months.  Second Febuary is not present.
  54.    //
  55.    cout << months << endl;
  56.    //
  57.    // Find the Number of days in June.
  58.    //
  59.    months_type::iterator p = months.find(string("June"));
  60.    //
  61.    // Print out the number of days in June.
  62.    //
  63.    if (p != months.end())
  64.      cout << endl << *p << endl;
  65.  
  66.    return 0;
  67.  }
  68.  
  69.  
  70.